package com.jiangcheng.demo.mapper;

import com.jiangcheng.demo.entity.Reply;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jiangcheng.demo.utils.FindReplysVo;
import com.jiangcheng.demo.utils.FindReplytoReplyByCommentId;
import org.apache.ibatis.annotations.Select;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2021-07-26
 */
public interface ReplyMapper extends BaseMapper<Reply> {

    @Select("SELECT COUNT(*) FROM reply where comment_id = #{commentId}")
    int findCountByCommentId(int commentId);

    @Select(("SELECT username,u_pic,r_id,content,u_id,comment_id,to_r_id,reply.created_time " +
            "FROM reply inner join user on user.id = reply.u_id where comment_id = #{commentId} AND to_r_id IS NULL "))
    List<FindReplysVo> findAllReplyByCommentId(int commentId);

    @Select("SELECT u1.username AS replyUsername,u1.u_pic AS replyUpic,\n" +
            "            r1.r_id AS replyReplyId,r1.content AS replyContent,r1.created_time,\n" +
            "            r2.content AS ByreplyContent,u2.username AS ByreplyUsername,u2.u_pic AS ByreplyUpic\n" +
            "            FROM reply AS r1,user AS u1,reply AS r2,user AS u2\n" +
            "            WHERE r1.u_id = u1.id AND r1.to_r_id = r2.r_id  AND r2.u_id = u2.id\n" +
            "            AND r1.comment_id = #{commentId} ")
    List<FindReplytoReplyByCommentId> findAllReplytoRepleByCommentId(int commentId);
}
